// sd_liferuning/pages/service/userauth-pay/index.js
var api = require("../../../../api.js"),app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   var that=this;
   app.request({
      url:api.default.userinfo,
      method: 'post',
      data:{
        uid:wx.getStorageSync('uid')
      },
      success:function(e){
        
        if (e.data.cashstatus != 1) {//查询保证金金额
          app.request({
            url: api.user.balance,
            data:{
              bid:wx.getStorageSync('bid')
            },
            success: function (res) {
              that.setData({
                money: res.money
              })
            }
          })
        } else {

          app.request({
            url: api.user.balance,
            data: {
              cid: wx.getStorageSync('cash').cid
            },

            success: function (res) {
              that.setData({
                money: res.money
              })
            }
          })

        }
        that.setData({
          status:e.data.cashstatus
        })
     
      }
   });
 
  
  },
  pay:function() {
    app.request({
      url: api.default.insertPrice,

      data:{
        uid:wx.getStorageSync('uid'),
        bid: wx.getStorageSync('bid'),

      },
      success:function(e){
         if(e.code==1){
            app.request({
              url: api.default.orderpay,
              data:{
                order_no: e.order_no,
                title:'保证金',
                uid: wx.getStorageSync('uid'),
              },
              success:function(res){
                if(res.code==1){
                  wx.requestPayment({
                    timeStamp:res.data.weixin.timeStamp,
                    nonceStr: res.data.weixin.nonceStr,
                    package: res.data.weixin.package,
                    signType: 'MD5',
                    paySign: res.data.weixin.paySign,
                    success:function(ress){
                      if(ress){
                        wx.showToast({
                          title: '支付成功',
                          duration:2000,
                          success:function(){
                              app.request({
                                url: api.default.userinfo,
                                method: 'post',
                                data:{
                                  uid:wx.getStorageSync('uid')
                                },
                                success:function(e){
                                  wx.setStorage({
                                    key: 'cash',
                                    data: e.data,
                                  })
                                 setTimeout(function(){
                                   wx.redirectTo({
                                     url: '../index/index',
                                   })
                                  
                                 },2000)
                                }
                              })
                          }
                        })
                      }
                    }
                  })
                }else{
                  wx.showModal({
                    title: '提示',
                    content: '支付失败',
                  })
                }
              }
            })
         }else{
           wx.showModal({
             title: '提示',
             content: '支付失败',
           })
         }
        }
     
    })
    
  },
  /**保证金退款 */
  outpay:function(){
    wx.showModal({
      title: '提示',
      content: '您确定要退还押金？',
      success:function(e){
        if(e.confirm){
          wx.showModal({
            title: '提示',
            content: '退款需要人工审核，退还到银行卡',
            success:function(res){
              app.request({
                url: api.user.bank,
                method: 'post',
                data: {
                  cid: wx.getStorageSync('cash').cid,
                },
                success: function (e) {
                  
                  if (e.cardnumber == null || e.name == null || e.cid == null) {
                   app.request({
                     url: api.order.out_balance,
                     method: 'post',
                     data:{
                       bid:wx.getStorageSync('bid'),
                       cid:wx.getStorageSync('cash').cid
                     },
                     success:function(res){
                       if(res.code==1){
                        wx.showToast({
                          title: '退款成功',
                          duration:2000,
                          success:function(){
                            app.request({
                              url: api.default.userinfo,
                              method: 'post',
                              data:{
                                uid: wx.getStorageSync('uid')
                              },
                              success:function(ress){
                                wx.setStorage({
                                    key: 'cash',
                                    data: ress.data,
                                 })
                                wx.redirectTo({
                                  url: '../auth-success/index',
                                })
                              }
                            })
                          }
                        })
                       }else{
                         wx.showModal({
                           title: '提示',
                           content: e.msg,
                         })
                       }
                     }
                   })

                  } else {
                    wx.showModal({
                      title: '提示',
                      content: '请先添加银行卡信息',
                      showCancel:false
                    })
                  }
                }
              })
            }
          })
        }else{
          console.log('-------');
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
    
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },
  

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})